/**
 * 
 */
package cn.suse.android.logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;

import cn.suse.android.entity.Circle;
import cn.suse.android.entity.FriendsRecord;
import cn.suse.android.entity.Page;
import cn.suse.android.entity.User;
import cn.suse.android.entity.Video;
import cn.suse.android.utils.StringUtils;

/**
 * @author liubin
 * Create DateTime:Oct 18, 2012 11:30:48 AM
 * Description:
 * @Version: 1.0.0
 */
public class FriendsRecordParse extends BaseParse<FriendsRecord> {

	/* (non-Javadoc)
	 * @see cn.suse.android.logic.BaseParse#parseData(java.lang.String)
	 */
	@SuppressWarnings("finally")
	@Override
	public List<FriendsRecord> parseData(String content) {
		// TODO Auto-generated method stub
		List<FriendsRecord> lists = null;
		try {
			JSONObject json = new JSONObject(content);
			lists = new ArrayList<FriendsRecord>();
			Page page = parsePage(json.getJSONObject("paging"));
			JSONArray jsonArray = json.getJSONArray("data");
			for (int j = 0; j <jsonArray .length(); j++) {
					FriendsRecord friendsRecord = new FriendsRecord();
					friendsRecord.setPaging(page);
					JSONObject jsonObject = jsonArray.getJSONObject(j);
					friendsRecord.setRid(jsonObject.getString("rid"));
					friendsRecord.setCtime(jsonObject.getString("ctime"));
					JSONObject userObject = jsonObject.getJSONObject("user");
					User user = new User();
					user.setUid(userObject.getString("uid"));
					user.setGender(userObject.getString("gender"));
					user.setName(userObject.getString("name"));
					user.setLogo50(userObject.getString("logo50"));
					friendsRecord.setUser(user);
					try {
						String directNum = jsonObject.getString("rnum");
						if(StringUtils.convertToInt(directNum)>=0){
							friendsRecord.setRnum(directNum);
						}
					} catch (Exception e) {
						friendsRecord.setRnum("0");
					}
					try {
						String commentNum = jsonObject.getString("cnum");
						if(StringUtils.convertToInt(commentNum)>=0){
							friendsRecord.setCnum(commentNum);
						}
					} catch (Exception e) {
						friendsRecord.setCnum("0");
					}
					JSONObject mainObject = jsonObject.getJSONObject("main");
					friendsRecord.setContent(jsonObject.getJSONObject("source").getJSONObject("main").getString("content"));
					JSONArray pics = mainObject.getJSONArray("pics");
					if(pics.length()>0){
						List<String> picList = new ArrayList<String>();
						for(int k=0;k<pics.length();k++){
							JSONObject src = pics.getJSONObject(k);
							picList.add(src.getString("src"));
						}
						friendsRecord.setPics(picList);
					}
					JSONArray videObjects = mainObject.getJSONArray("videos");
					if(videObjects.length()>0){
						List<Video> videos = new ArrayList<Video>();
						for(int i=0;i<videObjects.length();i++){
							Video video  = new Video();
							JSONObject videoObject =videObjects.getJSONObject(i);
							video.setImg(videoObject.getString("img"));
							video.setLink(videoObject.getString("link"));
							video.setName(videoObject.getString("name"));
							video.setSwf(videoObject.getString("swf"));
							videos.add(video);
						}
						friendsRecord.setVideos(videos);
					}
					lists.add(friendsRecord);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}finally{
			return lists;
		}
	}

}
